﻿Shader "TouchTable/BaseShader"
{
    Properties
    {
        [NoScaleOffset]
        _MainTex ("Main Texture [主纹理]", 2D) = "white" {}
        //主颜色
        _MainColor ("Main Color [主颜色]", Color) = (1.0, 1.0, 1.0, 1.0)
        //全局自发光振幅
		_EmissionFlickerAmplitude("Emission Flicker Amplitude [振幅]", float) = 0.5
		//全局自发光频率
		_EmissionFlickerFrequency("Emission Flicker Frequency [频率]", float) = 1.0
		//全局自发光初相
		_EmissionFlickerPrimaryPhase("Emission Flicker Primary Phase [初相]",float) = 0.0
		//全局自发光偏移
		_EmissionFlickerOffset("Emission Flicker Offset [偏移]", float) = 1.0
    }
    SubShader
    {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "CanUseSpriteAtlas"="true" "PreviewType"="Plane"}
        Cull Off Lighting Off ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;

            struct appdata
            {
                float4 vertex : POSITION;
                float4 color : COLOR;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.color = v.color;
                o.uv = v.uv;
                return o;
            }

            float4 _MainColor;
            float _EmissionFlickerAmplitude;
            float _EmissionFlickerFrequency;
            float _EmissionFlickerPrimaryPhase;
            float _EmissionFlickerOffset;

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 mainTex = tex2D(_MainTex, i.uv);

                float emissionFlickerTime = _Time.x * _EmissionFlickerFrequency;
				float emissionFlicker = (sin(emissionFlickerTime + _EmissionFlickerPrimaryPhase) * 0.5 + 0.5) * _EmissionFlickerAmplitude + _EmissionFlickerOffset;

                fixed3 final = mainTex.rgb * i.color * _MainColor.rgb * emissionFlicker;

                return fixed4(final.rgb, mainTex.a);
            }
            ENDCG
        }
    }
}
